package com.chaosj.tools.rules

import com.chaosj.tools.StockDiscoverTools

import java.util.concurrent.atomic.AtomicBoolean

class CombinationRule implements IAbnormalRule{
    static IAbnormalRule rule

    List<IAbnormalRule> rules = new ArrayList<>()

    @Override
    boolean execute(String stockName, List<StockDiscoverTools.DescriptionStock> stocks) {
        AtomicBoolean match = new AtomicBoolean(false)
        rules.stream().forEach({rule->
            if( rule.support(stockName,stocks) && rule.execute(stockName,stocks) ){
                match.set(true)
            }
        })
        return match.get()
    }

    @Override
    boolean support(String stockName, List<StockDiscoverTools.DescriptionStock> stocks) {
        return true
    }

    @Override
    String description(Object... stockName) {
        return null
    }

    static synchronized CombinationRule getInstance(){
        if( rule==null ) {
           rule = new CombinationRule()
        }
        return rule
    }

    void registerRule(IAbnormalRule... rule){
        for( IAbnormalRule r : rule ){
            rules.add(r)
        }
    }

    private CombinationRule(){}
}
